var clickSprite = cc.Sprite.extend({
    _rect : null,
    _thisScale :null,
    _isClick:null,
    _thisType:null,
    updateCount:null,
    updateEnable:null,
    ctor:function (aTexture,thisScale,thisType) {
        this._super(aTexture);
        this._isClick = false;
        this._rect = cc.rect(0, 0, this.width, this.height);
        this._thisScale = thisScale;
        this._thisType = thisType;
        //触摸监听
        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: this.onTouchBegan,
            onTouchMoved: this.onTouchMoved,
            onTouchEnded: this.onTouchEnded
        }, this);

        //计时器
        this.updateEnable = true;
        this.updateCount = 0;
        this.scheduleNew(function(){
            var clearTimeOut;
            switch(this._thisType){
                case 1:
                    clearTimeOut = 15;
                    break;
                case 2:
                    clearTimeOut = 10;
                    break;
                case 3:
                    clearTimeOut = 5;
                    break;
            }
            this.updateCount ++ ;
            if(this.updateCount == clearTimeOut){
                this.updateEnable = false;
                this.destroy();
            }
        },0.1);
    },
    scheduleNew:function(callback,interval){
        if(this.updateEnable){
            var then = Date.now();
            interval = interval * 1000;
            this.schedule(function(){
                var now = Date.now();
                var delta = now - then;
                if(delta > interval){
                    then = now - (delta % interval);
                    callback.call(this);
                }
            }.bind(this),0);
        }
    },
    destroy:function () {
        this.runAction(
            cc.spawn(
                cc.fadeOut(0.1),
                cc.sequence(
                    cc.delayTime(0.1),
                    cc.callFunc(this.deleteThis,this)
                )
            )
        );          
    },
    deleteThis:function(){
        cc.director.getRunningScene().children[0].removeChild(this);
    },
    onTouchBegan:function(touch,event){
        var that = event.getCurrentTarget();
        if(!that._isClick){
            var pos = touch.getLocation();
            if (!that.isTouchInRect(touch)){
                return false;
            }else{
                cc.director.getRunningScene().children[0].plusPoint(that.x,that.y,that._thisType);
                cc.audioEngine.playEffect(res.click_mp3);
                that._isClick = !that._isClick;
                that.destroy();
                return true;
            }
        }
    },
    onTouchEnded:function(touch,event){
    },
    onTouchMoved:function(touch,event){
    },
    getRect:function () {
        return cc.rect(-this._rect.width / 2 * this._thisScale, -this._rect.height / 2 * this._thisScale, this._rect.width * this._thisScale, this._rect.height * this._thisScale);
    },
    isTouchInRect:function (touch) {
        var getPoint = touch.getLocation();
        var myRect = this.getRect();
        myRect.x += this.x;
        myRect.y += this.y;
        return cc.rectContainsPoint(myRect, getPoint);
    }
});